package com.teamcenter.rac.massupdate.validators;

import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCComponentItemRevision;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.massupdate.Activator;
import com.teamcenter.rac.massupdate.model.UpdateJobModel;
import com.teamcenter.rac.massupdate.ui.Messages;
import com.teamcenter.rac.massupdate.utils.MassUpdateHelper;
import com.teamcenter.rac.massupdate.utils.SourceHelper;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;

public class ConfirmValidation extends AbstractMassUpdateValidator {
	private UpdateJobModel m_updateJobModel;

	public ConfirmValidation(UpdateJobModel paramUpdateJobModel) {
		this.m_updateJobModel = paramUpdateJobModel;
	}

	public IStatus validate(Object paramObject) {
		Object localObject;
		if ((this.m_updateJobModel.getChangeObject() == null) && (this.m_updateJobModel.isChangeSupported())) {
			StringBuffer _localObject = new StringBuffer();
			_localObject = ((StringBuffer) _localObject).append(Messages.ConfirmPage_Msg_CannotSaveAsUpdateNoChange);
			((StringBuffer) _localObject).append("\n");
			return new Status(1, Activator.getDefault().getBundleSymbolicName(),
					((StringBuffer) _localObject).toString());
		}
		if ((this.m_updateJobModel.getChangeObject() != null) && (this.m_updateJobModel.getMarkupObject() == null)) {
			localObject = (TCComponentItemRevision) SourceHelper
					.getAssociatedComponent(this.m_updateJobModel.getChangeObject(), getTCSession());
			boolean bool1 = MassUpdateHelper.evaluateCondition("isCMHasImpactedItemCreatableForPrimary",
					new TCComponent[] { (TCComponent) localObject }, getTCSession());
			int i = 0;
			if (localObject != null) {
				try {
					i = (((TCComponentItemRevision) localObject).okToModify()) && (bool1) ? 1 : 0;
				} catch (TCException localTCException1) {
				}
			}
			if (i == 0) {
				StringBuffer localStringBuffer = new StringBuffer();
				localStringBuffer = localStringBuffer
						.append(Messages.ConfirmPage_Msg_ChangeNotModifiableCannotSaveOrExecute);
				localStringBuffer.append("\n");
				return new Status(1, Activator.getDefault().getBundleSymbolicName(), localStringBuffer.toString());
			}
			return Status.OK_STATUS;
		}
		if ((this.m_updateJobModel.getChangeObject() != null) && (this.m_updateJobModel.getMarkupObject() != null)) {
			localObject = new StringBuffer();
			TCComponent localTCComponent = SourceHelper.getAssociatedComponent(this.m_updateJobModel.getMarkupObject(),
					getTCSession());
			TCComponentItemRevision localTCComponentItemRevision = (TCComponentItemRevision) SourceHelper
					.getAssociatedComponent(this.m_updateJobModel.getChangeObject(), getTCSession());
			boolean bool2 = MassUpdateHelper.evaluateCondition("isCMHasImpactedItemCreatableForPrimary",
					new TCComponent[] { localTCComponentItemRevision }, getTCSession());
			if (localTCComponent != null) {
				try {
					if ((!localTCComponent.okToModify()) || (!bool2)) {
						((StringBuffer) localObject).append("\n");
						localObject = ((StringBuffer) localObject)
								.append(Messages.ConfirmPage_Msg_UpdateNotModifiableCannotSave);
						((StringBuffer) localObject).append("\n");
					}
				} catch (TCException localTCException2) {
					localObject = ((StringBuffer) localObject)
							.append(Messages.ConfirmPage_Msg_CannotAccessUpdateCannotSave);
					((StringBuffer) localObject).append("\n");
				}
			}
			if (!((StringBuffer) localObject).toString().isEmpty()) {
				return new Status(1, Activator.getDefault().getBundleSymbolicName(),
						((StringBuffer) localObject).toString());
			}
		}
		return Status.OK_STATUS;
	}
}
